package Q819

class Solution {
    fun mostCommonWord(paragraph: String, banned: Array<String>): String {
        var paragraph = paragraph
        paragraph = paragraph.toLowerCase()
        paragraph = paragraph.replace('!', ' ').replace('?', ' ').replace('\'', ' ').replace(',', ' ').replace(';', ' ')
            .replace('.', ' ').trim { it <= ' ' }
        val split = paragraph.split(" ")


        val s = paragraph.split(" ".toRegex()).toTypedArray()

        val map1: MutableMap<String, Int> = HashMap()
        val map2: MutableMap<String, Int> = HashMap()

        for (s in banned) {
            map1[s] = 1
        }

        for (s in split) {
            if (!map1.containsKey(s) && s != "") {
                map2[s] = map2.getOrDefault(s, 0) + 1
            }
        }

        var max = -1
        var res: String? = null
        for (s1 in map2.keys) {
            if (map2[s1]!! > max) {
                max = map2[s1]!!
                res = s1
            }
        }
        return res.toString()
    }
}

fun main() {
    val s : String = "Bob hit a ball, the hit BALL flew far after it was hit."
    val banned : Array<String> = arrayOf("hit")

    println(Solution2().mostCommonWord(s, banned))
    println(Solution().mostCommonWord(s, banned))

    println()

    println(Test().test("ni hao zhe shi ce shi"))

}

internal class Solution2 {
    fun mostCommonWord(paragraph: String, banned: Array<String>): String? {
        var paragraph = paragraph
        paragraph = paragraph.toLowerCase()
        paragraph = paragraph.replace('!', ' ').replace('?', ' ').replace('\'', ' ').replace(',', ' ').replace(';', ' ')
            .replace('.', ' ').trim { it <= ' ' }
        val s = paragraph.split(" ".toRegex()).toTypedArray()
        val map1: MutableMap<String, Int> = HashMap()
        val map2: MutableMap<String, Int> = HashMap()
        for (str in banned) {
            map1[str] = 1
        }
        for (s1 in s) {
            if (!map1.containsKey(s1) && s1 != "") {
                map2[s1] = map2.getOrDefault(s1, 0) + 1
            }
        }
        var max = -1
        var res: String? = null
        for (s1 in map2.keys) {
            if (map2[s1]!! > max) {
                max = map2[s1]!!
                res = s1
            }
        }
        return res
    }
}

class Test{
    fun test(s : String) : String{
        var toLowerCase = s.toUpperCase()
        var split = toLowerCase.split(" ")
        for(s in split){
            println(s)
        }
        return toLowerCase
    }
}